﻿/*
 * @Autor: PK
 * @Date: 2021/12/26 21:07:SS
 */

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using ShkSimulation.core.dao.statistic;
using ShkSimulation.core.util;

namespace ShkSimulation.desktop.component.tools {

	public class DataReporterDisplay {
		
		public DataReporterDisplay() {
		}

		public DataReporterDisplay(DataReporter reporter, int index) {
			Index = index;
			UniqueKey = reporter.Uniquekey;
			Properties = CollectionUtil.StringLimitJoin(reporter.GetReportInfo().DataUniqueKeys, 20);
		}

		public int Index { get; private set; }
		public string UniqueKey { get; private set; }
		public string Properties { get; private set; }
		public bool Selected { get; set; } = true;

	}

	public class DataReporterBindingList : BindingList<DataReporterDisplay> {
		
		private int index = 1;

		public List<DataReporter> DataReporters { get; set; }
		
		public string GroupName { get; set; }
		
		public DataReporterBindingList(string name, ICollection<DataReporter> agencies, DataGridView reporterSelectView) {
			this.DataReporters = new List<DataReporter>(agencies);
			foreach (var agency in agencies) {
				Add(new DataReporterDisplay(agency, index));
				index++;
			}
			this.GroupName = name;
			this.ReporterSelectView = reporterSelectView;
			this.BindView(reporterSelectView);
		}
		
		private DataGridView ReporterSelectView { get; set; }

		private Dictionary<string, string> NameMapping = new Dictionary<string, string>() {
			{"UniqueKey", "唯一键"},
			{"Index", "序号"},
			{"Properties", "属性列表"},
		};

		public void BindView(DataGridView reporterSelectView) {
			reporterSelectView.AutoGenerateColumns = true;
			reporterSelectView.DataSource = this;
			DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
			cbHeader.OnCheckBoxClicked += Header_OnCheckBoxClicked;
			int headerIndex = reporterSelectView.Columns.Count - 1;
			reporterSelectView.Columns[headerIndex].HeaderCell = cbHeader;
			reporterSelectView.Columns[headerIndex].HeaderText = "";
			SetColumnHeader(reporterSelectView);
		}
		
		private void SetColumnHeader(DataGridView reporterSelectView) {
			foreach (DataGridViewColumn column in reporterSelectView.Columns) {
				if (NameMapping.ContainsKey(column.Name)) {
					column.HeaderText = NameMapping[column.Name];
				}
			}
		}

		private void Header_OnCheckBoxClicked(bool state) {
			ReporterSelectView.EndEdit();
			foreach (var agencyDisplay in this) {
				agencyDisplay.Selected = state;
			}
			ReporterSelectView.Refresh();
		}

	}
}